package com.test.mooan.labama.view_boom.widget_boom;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.test.mooan.labama.R;
import com.test.mooan.labama.utils_boom.Stringutil_boom;


public class VercodeInputView_boom extends LinearLayout {

    private final TextView[] tvs = new TextView[4];

    private int index = 0;

    private Runnable inputFinishRunnable;

    public VercodeInputView_boom(Context context) {
        super(context);
        initView(context);
    }

    public VercodeInputView_boom(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public VercodeInputView_boom(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context){
        View view = View.inflate(context,R.layout.view_vercode_input_boom,this);

        tvs[0] = view.findViewById(R.id.view_vercode_input_tv_1);
        tvs[1] = view.findViewById(R.id.view_vercode_input_tv_2);
        tvs[2] = view.findViewById(R.id.view_vercode_input_tv_3);
        tvs[3] = view.findViewById(R.id.view_vercode_input_tv_4);

        tvs[3].addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}
            @Override
            public void afterTextChanged(Editable s) {
                if (s.length()==1&&inputFinishRunnable!=null) inputFinishRunnable.run();
            }
        });

    }

    public String getVercode(){
        return Stringutil_boom.getSafeString(tvs[0].getText().toString())+
                Stringutil_boom.getSafeString(tvs[1].getText().toString())+
                Stringutil_boom.getSafeString(tvs[2].getText().toString())+
                Stringutil_boom.getSafeString(tvs[3].getText().toString());
    }

    public void inputNumber(String numStr){
        if (index>=0&&index<4){
            tvs[index].setText(numStr);
            index++;
            if (index>=0&&index<4) setInputTv(index);
        }
    }

    public void deleteNumber(){
        if (index>=1&&index<5){
            index--;
            tvs[index].setText("");
            if (index>=0&&index<4) setInputTv(index);
        }
    }

    public void setInputFinishRunnable(Runnable inputFinishRunnable) {
        this.inputFinishRunnable = inputFinishRunnable;
    }

    private void setInputTv(int i){
        if (i<0||i>3) return;
        for (int j = 0;j<tvs.length;j++){
            tvs[j].setBackgroundResource(i==j ? R.drawable.ic_border_blue_boom_1dp_round_corner_white_10dp:R.drawable.ic_round_corner_gray_llll_10dp);
        }
    }

}
